package com.cwh.taskcenter.task.gateway;

import com.cwh.taskcenter.domain.task.gateway.TaskRuleGateWay;
import com.cwh.taskcenter.domain.task.model.TaskRule;
import com.cwh.taskcenter.task.converter.TaskRuleConverter;
import com.cwh.taskcenter.task.mapper.TaskRuleMapper;
import com.cwh.taskcenter.task.model.TaskRuleDO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.stream.Collectors;


/**
 * @author cwh
 */
@Slf4j
@Component
public class TaskRuleGateWayImpl implements TaskRuleGateWay {

    @Autowired
    private TaskRuleMapper taskRuleMapper;

    @Override
    public void save(Long taskId,TaskRule taskRule) {
        TaskRuleDO taskRuleDO = TaskRuleConverter.toTaskRuleDO(taskRule);
        taskRuleDO.setTaskId(taskId);
        taskRuleMapper.create(taskRuleDO);
    }

    @Override
    public void saveBatch(Long taskId, List<TaskRule> taskRules) {
        List<TaskRuleDO> taskRuleDOList = taskRules.stream().map(
                taskRule -> {
                    TaskRuleDO taskRuleDO = TaskRuleConverter.toTaskRuleDO(taskRule);
                    taskRuleDO.setTaskId(taskId);
                    return taskRuleDO;}).collect(Collectors.toList());
        taskRuleMapper.creates(taskRuleDOList);
    }
}
